package com.easy.user.controller;

import com.easy.common.bean.User;
import com.easy.common.service.IUserService;
import com.easy.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/driver-approval")
public class DriverApprovalController {

    @Autowired
    IUserService userService;

    // 获取司机申请列表（状态为1的用户）
    @GetMapping("/applications")
    public Result getDriverApplications() {
        try {
            List<User> applications = userService.getDriverApplications(1);
            return Result.success(applications);
        } catch (Exception e) {
            return Result.fail(500, "获取司机申请列表失败: " + e.getMessage());
        }
    }

    // 批准司机申请
    @PostMapping("/approve/{id}")
    public Result approveDriverApplication(@PathVariable Integer id) {
        try {
            int result = userService.approveDriverApplication(id);
            if (result > 0) {
                return Result.success("司机申请已批准");
            } else {
                return Result.fail(500, "批准失败");
            }
        } catch (Exception e) {
            return Result.fail(500, "批准失败: " + e.getMessage());
        }
    }
    
    // 拒绝司机申请
    @PostMapping("/reject/{id}")
    public Result rejectDriverApplication(@PathVariable Integer id, @RequestBody(required = false) String reason) {
        try {
            int result = userService.rejectDriverApplication(id);
            if (result > 0) {
                return Result.success("司机申请已拒绝");
            } else {
                return Result.fail(500, "拒绝失败");
            }
        } catch (Exception e) {
            return Result.fail(500, "拒绝失败: " + e.getMessage());
        }
    }
}
